package com.caijing.caijingxueyuanxd.config;
// 在配置类中添加静态资源映射
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


import java.io.IOException;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射/static目录下的所有资源
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/*");
    }
    /**
     * 获取static目录下所有图片资源
     * @return 图片资源数组
     */
    public Resource[] getImageResources() throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        // 支持的图片格式
        String[] imageExtensions = {"png", "jpg", "jpeg", "gif", "bmp", "webp", "svg"};

        // 构建搜索模式（递归搜索所有子目录）
        String pattern = "classpath*:/static/**/*.{" + String.join(",", imageExtensions) + "}";

        return resolver.getResources(pattern);
    }
}